Specifying Registers for Local Variables

Contents|Index|Previous|Next
Specifying registers for local variables

You can define a local register variable with a specified register like the following.

register int *foo asm ("a5");

Here a5 is the name of the register which should be used. This is the same syntax used for defining global register variables, but for a local variable it would appear within a function.

Naturally the register name is CPU-dependent, but this is not a problem, since specific registers are most often useful with explicit assembler instructions (see Assembler instructions with C expression operands). Both of these things generally require that you conditionalize your program according to CPU type.

In addition, operating systems on one type of CPU may differ in how they name the registers; then you would need additional conditionals. For example, some 68000 operating systems call this register, %a5.

Eventually there may be a way of asking the compiler to choose a register automatically, but first we need to figure out how it should choose and how to enable you to guide the choice. No solution is evident.

Defining such a register variable does not reserve the register; it remains available for other uses in places where flow control determines the variable’s value is not live. However, these registers are made unavailable for use in the reload pass. Don’t be surprised if excessive use of this feature leaves the compiler too few available registers to compile certain functions.